﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Collections;
using System.Net;

using Microsoft.Web.Administration;

using King.Extensions;
using King.Reflection;
using King.Text;
using King.Automation;
using King.Declarative;

namespace King.InternetInformationService {

    /// <summary>
    /// Commands to install and uninstall IIS components.
    /// </summary>
    [CommandGroup]
    public abstract class ServerCommand : Command {
        protected static readonly int[] Warnings = new int[] { 
            3010 // reboot warning
        };

        protected static readonly string[] Packages = new string[] {
            "IIS-WebServerRole", "IIS-WebServer", "IIS-CommonHttpFeatures", "IIS-StaticContent", 
            "IIS-DefaultDocument", "IIS-DirectoryBrowsing", "IIS-HttpErrors", "IIS-HttpRedirect", 
            "IIS-ApplicationDevelopment", "IIS-ASPNET", "IIS-NetFxExtensibility", "IIS-ASP", "IIS-CGI", 
            "IIS-ISAPIExtensions", "IIS-ISAPIFilter", "IIS-ServerSideIncludes", "IIS-HealthAndDiagnostics",
            "IIS-HttpLogging", "IIS-LoggingLibraries", "IIS-RequestMonitor", "IIS-HttpTracing", 
            "IIS-CustomLogging", "IIS-ODBCLogging", "IIS-Security", "IIS-BasicAuthentication", 
            "IIS-WindowsAuthentication", "IIS-DigestAuthentication", "IIS-ClientCertificateMappingAuthentication", 
            "IIS-IISCertificateMappingAuthentication", "IIS-URLAuthorization", "IIS-RequestFiltering", 
            "IIS-IPSecurity", "IIS-Performance", "IIS-HttpCompressionStatic", "IIS-HttpCompressionDynamic",
            "IIS-WebServerManagementTools", "IIS-ManagementConsole", "IIS-ManagementScriptingTools", 
            "IIS-ManagementService", "IIS-IIS6ManagementCompatibility", "IIS-Metabase", "IIS-WMICompatibility",
            "IIS-LegacyScripts", "IIS-LegacySnapIn"
        };

        internal ServerCommand(params object[] arguments)
            : base(arguments) {
        }

        protected IEnumerable Execute(object commandLine) {

            return new TextSingleSpaced {
                new ConsoleFlush { new TextJoin(" ") { "pkgmgr.exe", commandLine } },
                new ConsoleYellow { new AutomationProcess("pkgmgr.exe", warnings: Warnings) { commandLine } },
            };
        }
    }
    public sealed class ServerUninstall : ServerCommand {

        [Command]
        internal ServerUninstall() {
        }

        public IEnumerable Execute(DeclarationContext context) {
            yield return Execute(new TextJoin(" ") { "/quiet", "/norestart", new TextJoin(";", "/uu:") { Packages } });
        }
    }
    public sealed class ServerInstall : ServerCommand {

        [Command]
        internal ServerInstall() {
        }

        public IEnumerable Execute(DeclarationContext context) {
            yield return Execute(new TextJoin(" ") { "/quiet", "/norestart", new TextJoin(";", "/iu:") { Packages } });
        }
    }
}
